﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Properties;

namespace Smarly.Web.Helpers
{
	public class MvcTag : IDisposable
	{
		private readonly ViewContext _viewContext;
		private readonly string _tag;
		private bool _disposed;

		public MvcTag(ViewContext viewContext, string tag)
		{
			if (viewContext == null)
			{
				throw new ArgumentNullException("viewContext");
			}

			_viewContext = viewContext;
			_tag = tag;
		}

		public void Dispose()
		{
			Dispose(true /* disposing */);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!_disposed)
			{
				_disposed = true;
				TagExtensions.EndTag(_viewContext, _tag);
			}
		}

		public void EndTag()
		{
			Dispose(true);
		}
	}
}